import json
import os


class GaiConfiguration:
    _username = "ft-user"
    _password = "77ae56c4b13c9b07ed5645c769cfebc4"

    @staticmethod
    def set_password(password: str):
        GaiConfiguration._password = password

    @staticmethod
    def set_username(username: str):
        GaiConfiguration._username = username

    @staticmethod
    def get_properties():
        use_debug_setting = "GAI_USE_DEBUG_SETTINGS" in os.environ
        path = GaiConfiguration.get_file_path()

        def setup_account(params):
            if GaiConfiguration._username is None or GaiConfiguration._password is None:
                raise RuntimeError(
                    "Usename/password must be set with AccountManager.set_account(). "
                    "Contact your system administrator if you do not know them.")
            params["hbase.client.username"] = GaiConfiguration._username
            params['hbase.client.password'] = GaiConfiguration._password
            return params

        if use_debug_setting:
            params = GaiConfiguration.get_debug_properties()
            params["hbase.client.username"] = "dmx"
            params['hbase.client.password'] = '123456'
            return params
        elif os.path.exists(path) and os.path.isfile(path):
            with open(path) as f:
                params = json.load(f)
                params = setup_account(params)
                return params
        else:
            params = GaiConfiguration.get_default_properties()
            params = setup_account(params)
            return params

    @staticmethod
    def get_default_properties():
        ret = GaiConfiguration.get_incomplete_properties()
        ret["hbase.thrift.server"] = "172.16.20.102"  # deprecated
        ret["hbase.thrift.port"] = 9090  # deprecated
        ret["ids2gid.api"] = "https://api-gateway.bi.getui.com/gt-feature-data-service/ordered/map-ids2gid"
        ret["retriever.api"] = "https://api-gateway.bi.getui.com/gt-feature-data-service/ordered/query-lite"
        return ret

    @staticmethod
    def get_debug_properties():
        ret = GaiConfiguration.get_incomplete_properties()
        ret["hbase.thrift.server"] = "192.168.10.193"  # deprecated
        ret["hbase.thrift.port"] = 9090  # deprecated
        ret["ids2gid.api"] = "http://192.168.10.193:9081/ordered/map-ids2gid"
        ret["retriever.api"] = "http://192.168.10.193:9081/ordered/query-lite"
        return ret

    @staticmethod
    def get_incomplete_properties():
        return {
            "hbase.thrift.server"  : "PLACEHOLDER",  # deprecated
            "hbase.thrift.port"    : "PLACEHOLDER",  # deprecated
            "hbase.client.username": "PLACEHOLDER",
            "hbase.client.password": "PLACEHOLDER",
            "ids2gid.api"          : "PLACEHOLDER",
            "retriever.api"        : "PLACEHOLDER",
            "ids2gid.table.name"   : "report_dw_mdp.dw_user_ids2gid_type",
            "verbose"              : "true",
            "engine"               : "spark",
            "log.path"             : "/tmp/gai_log"
        }

    @staticmethod
    def get_file_path():
        return "{}/{}".format(
            os.path.dirname(os.path.abspath(__file__)),
            "gai_conf.json")


class SparkConfiguration:
    @staticmethod
    def get_properties():
        path = SparkConfiguration._get_file_path()
        if os.path.exists(path) and os.path.isfile(path):
            with open(path) as f:
                return json.load(f)
        else:
            return {
                "partition.per.capacity": "20"
            }

    @staticmethod
    def _get_file_path():
        # Generates the path of spark configuration file that locates under
        # the same folder as this python script.
        return "{}/{}".format(
            os.path.dirname(os.path.abspath(__file__)),
            "spark_conf.json")


class FeatureRetrieverConfiguration:
    @staticmethod
    def get_properties():
        return {
            "sample.column.sep": "|",
            "log.path"         : "/tmp/gai_log",
            "data.source"      : "hbase",
            "month.range"      : "0",
            "query.batch.size" : 500
        }


class IdMapperConfiguration:
    @staticmethod
    def get_properties():
        return {
            "sample.column.sep" : "|",
            "ids2gid.table.name": "report_dw_mdp.dw_user_ids2gid_type",
            "data.source"       : "hbase",
            "ids2gid.batch.size": 500
        }
